<?php
/**
 * Created by PhpStorm.
 * User: dream
 * Date: 2017/8/19
 * Time: 19:53
 */
//基础公共Controller
class MY_Controller extends CI_Controller
{

    const FUNC_ONE_ARGS = 1; //函数参数为1个
    const FUNC_TWO_ARGS = 2; //函数参数为2个
    const REQUEST_GET = "get"; //get请求
    const REQUEST_POST = "post"; //post请求
    const PER_PAGE = 10;
    public function __construct()
    {
        parent::__construct();
    }

    //获取get参数
    protected function get($query_string_key)
    {

        return $this->input->get($query_string_key);
    }

    //获取post参数
    protected function post($query_string_key)
    {

        return $this->input->post($query_string_key);
    }

    //使用session
    protected function session()
    {
        $func_args_num = func_num_args();
        $this->load->library("session");
        if($func_args_num==self::FUNC_ONE_ARGS)
        {
            $func_keys = func_get_args();
            $session_key = reset($func_keys);
            return $this->session->userdata($session_key);
        }else if($func_args_num==self::FUNC_TWO_ARGS)
        {
            list($session_key,$session_val) = func_get_args();
            $this->session->set_userdata($session_key,$session_val);
        }
    }
    //加载类库
    protected function loadLibrary($lib_names)
    {
       $this->_loadAsset($lib_names);
    }
    //判断是否是get请求
    protected function isGet()
    {
        $req_method = strtolower($this->reqMethod());
        if($req_method==self::REQUEST_GET)
            return true;
    }
    //获取请求方法
    protected function reqMethod()
    {
        $req_method = $this->input->method();
        return $req_method;
    }
    //判断是否是post请求
    protected function isPost()
    {
        $req_method = strtolower($this->reqMethod());
        if($req_method==self::REQUEST_POST)
            return true;
    }

    //加载辅助函数

    protected  function loadHelper($helper_names)
    {
         $this->_loadAsset($helper_names,true);
    }

    private function _loadAsset($assets,$helper=false)
    {
        if(is_array($assets))
        {
            foreach ($assets as $asset)
            {
                if($helper)
                $this->load->helper($asset);
                else
                    $this->load->library($asset);
            }

        }
        else
        {
            if($helper)
                $this->load->helper($assets);
            else
                $this->load->library($assets);
        }


    }


    //分页 bootstrap
    protected function bootstrapPagination($base_url,$count_result,$per_page,$show_count=true)
    {
        $this->loadLibrary("bootstrap");

        return $this->bootstrap->getLinks($base_url,$count_result,$per_page,$show_count);
    }

    //加载视图
    protected function loadView($view_paths,$view_data=[])
    {
        if(is_array($view_paths))
        {
            foreach ($view_paths as $view_path)
                $this->load->view($view_path,$view_data);
        }
        else
            $this->load->view($view_paths,$view_data);
    }
    //加载模型 model

    protected function loadModel($models)
    {
        if(is_array($models))
        {
            foreach ($models as $model)
            {
                    $this->load->model($model);
            }
        }
        else
                $this->load->model($models);
    }

    //生成url地址
    protected  function url($url,$admin=false)
    {
        $this->loadHelper("url");
       return web_url($url,$admin);
    }

    protected function redirect($url)
    {
        $this->loadHelper("url");
         redirect($url);
    }
    //返回当前时间
    protected function time($need_format=true)
    {
       if($need_format)
       {
           return date("Y-m-d H:i:s",time());
       }
       else
           return time();
    }
    //获取ip信息
    protected function get_ip_info($search_ip)
    {
        $ip = @file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".$search_ip);
        $ip = json_decode($ip,true);

        if($ip['code']==1){
            return false;
        }else
        {
            return ['country'=>$ip['data']['country'],'city'=>$ip['data']['city'],'isp'=>$ip['data']['isp']];
        }

    }

    //上传文件

    protected function upload_file($config,$form_file_name)
    {

        $this->load->library('upload', $config);

        $this->upload->initialize($config);

        if (!$this->upload->do_upload($form_file_name))
        {
            $this->upload->display_errors('', '');
            $error = $this->upload->display_errors();
            return $error;
        }
        else
        {
            $data = array($form_file_name => $this->upload->data());
            return $data;
        }
    }

    //获取博主信息
    protected function getUserInfo($session_id=false)
    {
        $this->loadHelper("url");

        $this->loadModel("userinfo");

        $where_id  = $session_id?$this->session('user')->id:ADMIN_ID;

        $db_users = $this->userinfo->getWhere(["id"=>$where_id]);

        $db_user = reset($db_users); //信息表
        $data_arr = ['db_user'=>$db_user];
        return $data_arr;
    }

    //分页
    protected function _pagination($base_url,$count,$per_page,$views_arr,$view_data)
    {
        $this->loadHelper("url");
        $links_str = $this->bootstrapPagination($base_url,$count,$per_page,true);
        $view_data['links']=$links_str;
        $this->loadView($views_arr,$view_data);
    }
     //时间段查询
    protected function _time_search($base_url)
    {

        $start_time= $this->input->get_post("start_time");
        $end_time = $this->input->get_post("end_time");
        $user_id = $this->session("user")->id;
        if(empty($start_time)||empty($end_time))
        {
            $start_time = urldecode($this->uri->segment(4));
            $end_time = urldecode($this->uri->segment(5));
        }
        $where = ["ctime>="=>$start_time,"ctime<="=>$end_time,"user_id"=>$user_id];
        $base_url = web_url($base_url.$start_time.'/'.$end_time,true);
        $data['where']=$where;
        $data['base_url']=$base_url;
        return $data;
    }
     //搜索
    protected function _search_key_word($base_url,$db_column)
    {
        $key_word = $this->input->get_post("key_word");
        $user_id = $this->session("user")->id;
        if(empty($key_word))
            $key_word = $this->uri->segment(4);
        $base_url = web_url($base_url.$key_word,true);
        $where = [$db_column=>$key_word,"user_id"=>$user_id];
        $data['where'] = $where;
        $data['base_url'] = $base_url;
        return $data;
    }

}